package be.dioubs.sportTrack.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import be.dioubs.sportTrack.dto.TrackDTO;

public class Converter {

	public Converter() {
	
	}
	
	public final static TrackDTO extrasToTrackDto(Bundle extras) {
		TrackDTO trackDTO = new TrackDTO();

		// getLong returns 0L if matching key is not found
		Long id = extras.getLong(TrackDTO.KEY_ID);
		if(id != 0)
			trackDTO.setId(id);
		
        trackDTO.setTitle(extras.getString(TrackDTO.KEY_TITLE));
        trackDTO.setDescription(extras.getString(TrackDTO.KEY_DESCRIPTION));
        trackDTO.setDate(new Date(extras.getLong(TrackDTO.KEY_DATE)));
        trackDTO.setDistanceNb(extras.getLong(TrackDTO.KEY_DISTANCE_NB));
        trackDTO.setDistanceUnit(extras.getString(TrackDTO.KEY_DISTANCE_UNIT));
        trackDTO.setFitnessLevel(extras.getString(TrackDTO.KEY_FITNESS_LEVEL));
        trackDTO.setHeartRateMax(extras.getString(TrackDTO.KEY_HEART_RATE_MAX));
        trackDTO.setHeartRateMoy(extras.getString(TrackDTO.KEY_HEART_RATE_MOY));
        trackDTO.setSport(extras.getString(TrackDTO.KEY_SPORT));
        trackDTO.setTime(extras.getString(TrackDTO.KEY_TIME));
        
		return trackDTO;
	}
	
	public final static Bundle trackDtoToExtras(TrackDTO trackDTO) {
		Bundle extras = new Bundle();

		extras.putString(TrackDTO.KEY_TITLE, trackDTO.getTitle());
		extras.putString(TrackDTO.KEY_DESCRIPTION, trackDTO.getDescription());
		extras.putDouble(TrackDTO.KEY_DATE, trackDTO.getDate().getTime());
		extras.putString(TrackDTO.KEY_SPORT, trackDTO.getSport());
		extras.putString(TrackDTO.KEY_TIME, trackDTO.getTime());
		extras.putLong(TrackDTO.KEY_DISTANCE_NB, trackDTO.getDistanceNb());
		extras.putString(TrackDTO.KEY_DISTANCE_UNIT, trackDTO.getDistanceUnit());
		extras.putString(TrackDTO.KEY_HEART_RATE_MOY, trackDTO.getHeartRateMoy());
		extras.putString(TrackDTO.KEY_HEART_RATE_MAX, trackDTO.getHeartRateMax());
		extras.putString(TrackDTO.KEY_FITNESS_LEVEL, trackDTO.getFitnessLevel());	
		
		return extras;
	}
	
	public final static ContentValues trackDtoToContentValues(TrackDTO trackDTO) {
		ContentValues contentValues = new ContentValues();

		Long id = trackDTO.getId();
		if(id != null)
			contentValues.put(TrackDTO.KEY_ID, trackDTO.getId());
		
		contentValues.put(TrackDTO.KEY_TITLE, trackDTO.getTitle());
		contentValues.put(TrackDTO.KEY_DESCRIPTION, trackDTO.getDescription());
		contentValues.put(TrackDTO.KEY_SPORT, trackDTO.getSport());
		contentValues.put(TrackDTO.KEY_DATE, trackDTO.getDate().getTime());
		contentValues.put(TrackDTO.KEY_DISTANCE_NB, trackDTO.getDistanceNb());
		contentValues.put(TrackDTO.KEY_DISTANCE_UNIT, trackDTO.getDistanceUnit());
		contentValues.put(TrackDTO.KEY_TIME, trackDTO.getTime());
		contentValues.put(TrackDTO.KEY_HEART_RATE_MOY, trackDTO.getHeartRateMoy());
		contentValues.put(TrackDTO.KEY_HEART_RATE_MAX, trackDTO.getHeartRateMax());
		contentValues.put(TrackDTO.KEY_FITNESS_LEVEL, trackDTO.getFitnessLevel());
		
		return contentValues;
	}
	
	public final static void cursorToIntentExtras(Cursor cursor, Intent intent) {
        intent.putExtra(TrackDTO.KEY_ID, cursor.getLong(cursor.getColumnIndexOrThrow(TrackDTO.KEY_ID)));
        intent.putExtra(TrackDTO.KEY_TITLE, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_TITLE)));
        intent.putExtra(TrackDTO.KEY_DESCRIPTION, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DESCRIPTION)));
        intent.putExtra(TrackDTO.KEY_SPORT, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_SPORT)));
        intent.putExtra(TrackDTO.KEY_DATE, cursor.getLong(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DATE)));
        intent.putExtra(TrackDTO.KEY_DISTANCE_NB, cursor.getLong(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DISTANCE_NB)));
        intent.putExtra(TrackDTO.KEY_DISTANCE_UNIT, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DISTANCE_UNIT)));
        intent.putExtra(TrackDTO.KEY_HEART_RATE_MOY, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_HEART_RATE_MOY)));
        intent.putExtra(TrackDTO.KEY_HEART_RATE_MAX, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_HEART_RATE_MAX)));
        intent.putExtra(TrackDTO.KEY_TIME, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_TIME)));
        intent.putExtra(TrackDTO.KEY_FITNESS_LEVEL, cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_FITNESS_LEVEL)));
	}		
	
	public final static List<TrackDTO> cursorToTrackDtos(Cursor cursor) {
		List<TrackDTO> trackDtos = new ArrayList<TrackDTO>();
		
		if(cursor.moveToFirst())
			do {
				TrackDTO trackDTO = new TrackDTO();

		        trackDTO.setId(cursor.getLong(cursor.getColumnIndexOrThrow(TrackDTO.KEY_ID)));
		        trackDTO.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_TITLE)));
		        trackDTO.setDescription(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DESCRIPTION)));
		        trackDTO.setSport(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_SPORT)));
		        trackDTO.setDistanceNb(cursor.getLong(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DISTANCE_NB)));
		        trackDTO.setDistanceUnit(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DISTANCE_UNIT)));
		        trackDTO.setHeartRateMoy(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_HEART_RATE_MOY)));
		        trackDTO.setHeartRateMax(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_HEART_RATE_MAX)));
		        trackDTO.setTime(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_TIME)));
		        trackDTO.setFitnessLevel(cursor.getString(cursor.getColumnIndexOrThrow(TrackDTO.KEY_FITNESS_LEVEL)));				

		        Long dateLong = cursor.getLong(cursor.getColumnIndexOrThrow(TrackDTO.KEY_DATE));
		        trackDTO.setDate(new Date(dateLong));		        
		        
				trackDtos.add(trackDTO);
			} while(cursor.moveToNext());
		
		return trackDtos;
	}
}
